package com.qxsx.midi.activity.main;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import com.qxsx.midi.R;
import com.qxsx.midi.fragment.ExamFragment;
import com.qxsx.midi.fragment.HomeFragment;
import com.qxsx.midi.fragment.MeFragment;
import com.qxsx.midi.fragment.StudyFragment;

import java.util.HashMap;


public class MainActivity extends FragmentActivity implements View.OnClickListener {


    public static final String TAG_1 = "HOME";
    public static final String TAG_2 = "STUDY";
    public static final String TAG_3 = "EXAM";
    public static final String TAG_4 = "ME";

    private TabHost mTabHost;
    private TabManager mTabManager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tabs);

        mTabHost = (TabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup();

        mTabManager = new TabManager(this, mTabHost, R.id.realtabcontent);

        mTabManager.addTab(mTabHost.newTabSpec(TAG_1).setIndicator(createTabView("首页", R.drawable.tabbar_home_selector)), HomeFragment.class, null);
        mTabManager.addTab(mTabHost.newTabSpec(TAG_2).setIndicator(createTabView("学音乐", R.drawable.tabbar_study_selector)), StudyFragment.class, null);
        mTabManager.addTab(mTabHost.newTabSpec(TAG_3).setIndicator(createTabView("考级", R.drawable.tabbar_exam_selector)), ExamFragment.class, null);
        mTabManager.addTab(mTabHost.newTabSpec(TAG_4).setIndicator(createTabView("我的", R.drawable.tabbar_me_selector)), MeFragment.class, null);

        if (savedInstanceState != null) {
            mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab"));
        }


    }





    private View createTabView(String text, int icon_id) {

        View view = LayoutInflater.from(this).inflate(R.layout.tab_indicator, null);

        TextView tv = (TextView) view.findViewById(R.id.tv_title);
        tv.setText(text);

        ImageView iv = (ImageView) view.findViewById(R.id.iv_icon);
        iv.setImageResource(icon_id);

        return view;
    }


    @Override
    public void onClick(View v) {

    }

    /**
     * This is a helper class that implements a generic mechanism for
     */
    public static class TabManager implements TabHost.OnTabChangeListener {
        private final FragmentActivity mActivity;
        private final TabHost mTabHost;
        private final int mContainerId;
        private final HashMap<String, TabInfo> mTabs = new HashMap<String, TabInfo>();
        TabInfo mLastTab;

        static final class TabInfo {
            private final String tag;
            private final Class<?> clss;
            private final Bundle args;
            private Fragment fragment;

            TabInfo(String _tag, Class<?> _class, Bundle _args) {
                tag = _tag;
                clss = _class;
                args = _args;
            }
        }

        static class DummyTabFactory implements TabHost.TabContentFactory {
            private final Context mContext;

            public DummyTabFactory(Context context) {
                mContext = context;
            }

            @Override
            public View createTabContent(String tag) {
                View v = new View(mContext);
                v.setMinimumWidth(0);
                v.setMinimumHeight(0);
                return v;
            }
        }

        public TabManager(FragmentActivity activity, TabHost tabHost, int containerId) {
            mActivity = activity;
            mTabHost = tabHost;
            mContainerId = containerId;
            mTabHost.setOnTabChangedListener(this);
        }

        public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) {
            tabSpec.setContent(new DummyTabFactory(mActivity));
            String tag = tabSpec.getTag();

            TabInfo info = new TabInfo(tag, clss, args);

            info.fragment = mActivity.getSupportFragmentManager().findFragmentByTag(tag);

            mTabs.put(tag, info);
            mTabHost.addTab(tabSpec);
        }

        @Override
        public void onTabChanged(String tabId) {
            TabInfo newTab = mTabs.get(tabId);
            if (mLastTab != newTab) {
                FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction();

                if (mLastTab != null) {
                    if (mLastTab.fragment != null) {
                        ft.hide(mLastTab.fragment);
                    }
                }
                if (newTab != null) {
                    if (newTab.fragment == null) {
                        newTab.fragment = Fragment.instantiate(mActivity, newTab.clss.getName(), newTab.args);
                        ft.add(mContainerId, newTab.fragment, newTab.tag);
                    } else {
                        ft.show(newTab.fragment);
                    }
                }

                mLastTab = newTab;
                ft.commit();
                mActivity.getSupportFragmentManager().executePendingTransactions();
            }
        }
    }

}
